package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	import keyboard.KeyboardFactory;
	
	import loader.LoaderFactory;
	
	import mapEditor.MapEditorManager;
	import mapEditor.MapEditorView;
	
	import render.RenderManager;
	
	import stages.StageReflection;
	
	import utils.ApplicationDomainUtil;
	
	[SWF(width = "1000",height = "600",frameRate = "60")]
	public class FLEditor extends Sprite
	{
		/**
		 *地图编辑器视图 
		 */		
		private var _mapEditorView:MapEditorView;
		
		public function FLEditor()
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			StageReflection.register(stage);
			RenderManager.instance.stage=stage;
			KeyboardFactory.Instance.register(stage);
			ApplicationDomainUtil.applicationDomain=ApplicationDomain.currentDomain;
			var loaderContext:LoaderContext=new LoaderContext();
			loaderContext.applicationDomain=ApplicationDomain.currentDomain;
			if(loaderContext.hasOwnProperty("allowLoadBytesCodeExecution")) loaderContext["allowLoadBytesCodeExecution"]=true;
			LoaderFactory.Instance.register(loaderContext,0);
			MapEditorManager.instance.register();
			
			StageReflection.stage.addEventListener(Event.RESIZE, __onStateResize);
			drawFrame();
			runMapEditor();
		}
		
		private function __onStateResize(evt:Event):void
		{
			drawFrame();
		}
		
		/**
		 *绘制运行窗体边框 
		 * 
		 */		
		private function drawFrame():void
		{
			graphics.clear();
			graphics.lineStyle(1, 0x525252);
			graphics.moveTo(0, 61);
			graphics.lineTo(0, StageReflection.stageHeight-1);
			graphics.lineTo(StageReflection.stageWidth-1, StageReflection.stageHeight-1);
			graphics.lineTo(StageReflection.stageWidth-1, 61);
			graphics.lineStyle(1, 0xb1b1b1);
			graphics.moveTo(1, 61);
			graphics.lineTo(1, StageReflection.stageHeight-2);
			graphics.lineTo(StageReflection.stageWidth-2, StageReflection.stageHeight-2);
			graphics.lineTo(StageReflection.stageWidth-2, 61);
			graphics.endFill();
		}
		
		/**
		 *运行地图编辑器 
		 * 
		 */		
		private function runMapEditor():void
		{
			if(_mapEditorView)_mapEditorView.dispose();
			_mapEditorView=null;
			
			_mapEditorView=new MapEditorView();
			addChild(_mapEditorView);
		}
	}
}